package no.nordicsemi.android.dfuupdate.dfu;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import no.nordicsemi.android.dfuupdate.R;
import no.nordicsemi.android.dfuupdate.bean.TagListData;
import no.nordicsemi.android.dfuupdate.dfu.adapter.TagListAdapter;

public class TagListActivity extends AppCompatActivity {

    RecyclerView rvList;
    RefreshLayout swipeRefreshLayout;
    TagListAdapter tagListAdapter;
    int pageNo = 0;
    List<TagListData> dataList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dfu_list);
        initData();
        initView();
    }

    private void initView() {
        final Toolbar toolbar  = findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        rvList = (RecyclerView)findViewById(R.id.rv_list);
        swipeRefreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
        rvList.setLayoutManager(new LinearLayoutManager(TagListActivity.this));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this );
        //设置布局管理器
        rvList.setLayoutManager(layoutManager);
        rvList.addItemDecoration(new DividerItemDecoration(TagListActivity.this,DividerItemDecoration.VERTICAL));
        //设置Adapter
        tagListAdapter = new TagListAdapter(dataList) ;
        rvList.setAdapter(tagListAdapter);
        tagListAdapter.setOnItemClickListener(new TagListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(TagListActivity.this,"click " + position + " item", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
//                upData(pageNo*5,(pageNo+1)*5);
//                tagListAdapter.notifyDataSetChanged();
                refreshlayout.finishRefresh(200);
            }
        });
        swipeRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNo++;
                Log.i("data===",""+pageNo);
                // getMonthList(type,pageNo);
                AddData(pageNo*10,(pageNo+1)*10);
                tagListAdapter.notifyDataSetChanged();
                refreshlayout.finishLoadmore(200);
            }
        });
    }


    private void initData() {

        for(int i=0;i<10;i++){
            TagListData tagListData = new TagListData();
            tagListData.setTagName("TAG_"+i);
            tagListData.setTagAddress(""+i);
            if(i%2 == 0){
                tagListData.setTagState(1);
                tagListData.setUpdate(true);
            }else{
                tagListData.setTagState(0);
                tagListData.setUpdate(false);
            }
            dataList.add(tagListData);
        }
    }

    private void AddData(int start,int end) {
       // dataList.clear();
        for(int i=start;i<end;i++){
            TagListData tagListData = new TagListData();
            tagListData.setTagName("TAG_"+i);
            tagListData.setTagAddress(""+i);
            if(i%2 == 0){
                tagListData.setTagState(1);
                tagListData.setUpdate(true);
            }else{
                tagListData.setTagState(0);
                tagListData.setUpdate(false);
            }
            dataList.add(tagListData);
        }
    }

    private void upData(int start,int end) {
        for(int i=start;i<end;i++){
            TagListData tagListData = new TagListData();
            tagListData.setTagName("TAG_"+i);
            tagListData.setTagAddress(""+i);
            if(i%2 == 0){
                tagListData.setTagState(1);
                tagListData.setUpdate(true);
            }else{
                tagListData.setTagState(0);
                tagListData.setUpdate(false);
            }
            dataList.add(tagListData);
        }
    }
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        if(android.R.id.home == (item.getItemId())) {
            onBackPressed();
        }
        return true;
    }

}
